
/*
 * @Descripttion: 限制IP中间件
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-24 17:49:16
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-26 20:18:21
 */

module.exports = (options, app) => {
    return async function forbidIp(ctx, next) {
        var forbid =false;
        //要屏蔽的ip 
        //1.从数据库获取 2.配置文件导入
        var arr =['127.0.0.2','127.0.0.3'];
        if(options.enable ===true){
            if (arr.indexOf(ctx.request.ip)!=-1) {
                forbid =true;
            }
        } 
        if(forbid === true){            
            var ss =`${new Date().toLocaleString()} [${ctx.ip}] [${ctx.method} ${ctx.url}] : ${ctx.app.enums.response_code_enum.forbid.msg} \n`;
            s =ctx.app.config.console.request.style+ss+ctx.app.config.console.request.style;
            console.log(s.bgMagenta);
            ctx.requestLogger.error(ctx.app.enums.response_code_enum.forbid.msg);
            ctx.outError(null,null,ctx.app.enums.response_code_enum.forbid);  
        }else{
            await next();
        }    
    }  
}